export default function debounce(fn: Function, delay = 500, immediate: Boolean = true) {
  if (
    Object.prototype.toString.call(fn) !== "[object Function]" &&
    Object.prototype.toString.call(fn) !== "[object AsyncFunction]"
  )
    throw Error("请传入函数!")
  let timer: number | null = null
  return function (this: any, ...args: any) {
    if (timer) clearTimeout(timer)
    if (immediate) {
      let callNow = !timer
      timer = setTimeout(() => {
        timer = null
      }, delay)
      //如果callNow为true,即原本timer为null
      //那么执行func函数
      if (callNow) fn.apply(this, args)
    } else {
      timer = setTimeout(() => {
        console.log("\x1b[32m 防抖程序启动 \x1b[0m")
        fn.call(this, ...args)
      }, delay)
    }
  }
}
